from django.shortcuts import render_to_response
from django.template import RequestContext



# --- mobilizy!!! ------ START
# http://mobiforge.com/developing/story/build-a-mobile-and-desktop-friendly-application-django-15-minutes
# list of mobile User Agents
mobile_uas = [
    'w3c ','acs-','alav','alca','amoi','audi','avan','benq','bird','blac',
    'blaz','brew','cell','cldc','cmd-','dang','doco','eric','hipt','inno',
    'ipaq','java','jigs','kddi','keji','leno','lg-c','lg-d','lg-g','lge-',
    'maui','maxo','midp','mits','mmef','mobi','mot-','moto','mwbp','nec-',
    'newt','noki','oper','palm','pana','pant','phil','play','port','prox',
    'qwap','sage','sams','sany','sch-','sec-','send','seri','sgh-','shar',
    'sie-','siem','smal','smar','sony','sph-','symb','t-mo','teli','tim-',
    'tosh','tsm-','upg1','upsi','vk-v','voda','wap-','wapa','wapi','wapp',
    'wapr','webc','winw','winw','xda','xda-'
    ]
 
mobile_ua_hints = [ 'SymbianOS', 'Opera Mini', 'iPhone', 'Android', 'MeeGo' ]


mobile_templates = {
    "new_payment.html" : "mobile/mnew_payment.html",
    "transfer.html" : "mobile/mtransfer.html",
    }


cli_templates = {
    "payments.html" : "cli/cli_payments.html",
    }
 
def mobileBrowser(request):
    ''' Super simple device detection, returns True for mobile devices '''
 
    mobile_browser = False
    print request.META['HTTP_USER_AGENT']
    ua = request.META['HTTP_USER_AGENT'].lower()[0:4]
 
    if (ua in mobile_uas):
        mobile_browser = True
    else:
        for hint in mobile_ua_hints:
            if request.META['HTTP_USER_AGENT'].find(hint) > 0:
                mobile_browser = True
 
    return mobile_browser
# --- mobilizy!!! ------ END


def render_agent(request, template_name, context={}, context_instance=None):
    mobilemode = mobileBrowser(request)
    #mobilemode = True # FOR TESTING
    
    context['mobile'] = False
    context['extends_template'] = 'content.html'
    
    if mobilemode:
        context['mobile'] = True
        context['extends_template'] = 'mobile/mcontent.html'
        
        if template_name in mobile_templates:
            template_name = mobile_templates[template_name]
        
    if request.session.get('cli', False):
        context['cli'] = True
        context['extends_template'] = 'cli/cli_content.html'
        context_instance = RequestContext(request)
        if template_name in cli_templates:
            template_name = cli_templates[template_name]
    
    return render_to_response(template_name, context, context_instance)
